<?php
/**
 * Description: 获取中奖信息
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/6/16
 * Time: 15:32
 */

use Biz\model\NewProduct\NewProductJdCardModel;
use Biz\model\NewProduct\NewProductWikiModel;
use Biz\model\NewProduct\NewProductWinnerListModel;
use Doraemon\tools\ImgUrl;

class GetLotteryInfoAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        $uid = $this->uid;
        $nid = $this->getRequest()->getQuery('nid', 0);
        if (!$nid || !$uid) {
            throw new \Exception('非法传参');
        }
        $nid = NewProductWikiModel::first(['id' => $nid], 'id');
        if (!$nid) {
            throw new \Exception('商品不存在');
        }

        $id = NewProductWinnerListModel::get('id', ['nid' => $nid, 'uid' => $uid]);
        if (!$id) {
            throw new \Exception('您未参加此活动');
        }
        $where['id'] = $nid;
        $where['status'] = NewProductWikiModel::STATUS_PASS;
        $where['is_delete'] = NewProductWikiModel::NO_DELETED;
        $where['end_time[<]'] = date('Y-m-d H:i:s');
        $info = NewProductWikiModel::first($where);

        if (is_null($info)) {
            throw new \Exception('活动不存在');
        }
        if ((int)$info['type'] === NewProductWikiModel::SUPER_PDT) {
            $data['prize_name'] = $info['prize_name'];
            $data['price'] = $info['price'] ? bcdiv($info['price'], 100, 2) : "0.00";
            $data['back_img'] = 'http://img.hhz1.cn/Op-imageShow/c924630t61250dW00qtnson?iv=1';
            $data['img'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $info["img"], ImgUrl::IMAGE_SIZE_SQ_PHONE) ?? '';
            $data['type'] = $info['type'];
            $data['nid'] = $nid;
            $data['winner_id'] = $id;
        } else {
            $data['jd_num'] = NewProductWinnerListModel::get('jd_num', ['nid' => $nid, 'uid' => $uid]) ?: null;
            //获取jd卡
            $data['prize_name'] = "JD|购物卡";
            $data['price'] = 50;
            $data['back_img'] = 'http://img.hhz1.cn/Op-imageShow/c924630t61250dW00qtnson?iv=1';
            $data['img'] = NewProductWinnerListModel::JD_IMG;
            $data['type'] = $info['type'];
            $data['nid'] = $nid;
            $data['jd_link'] = "https://item.jd.com";
            $data['winner_id'] = $id;
        }
        $this->setSuccess($data);
    }
}